﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title>查询</title>

    <link href="JS/EasyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="JS/EasyUI/themes/default/datagrid.css" rel="stylesheet" type="text/css" />
    <link href="JS/EasyUI/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="JS/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="JS/EasyUI/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="JS/EasyUI/locale/easyui-lang-zh_CN.js" type="text/javascript" charset="UTF-8"></script>
    <script src="JS/SearchEasyUI.js" type="text/javascript"></script>
    <script src="../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../Scripts/config.js" type="text/javascript"></script>
    <script src="../Scripts/EasyUIUtility.js" type="text/javascript"></script>
    <script language="JavaScript" src="JScript.js" type="text/javascript"></script>
    <script src="../Comm/JS/Calendar/WdatePicker.js" type="text/javascript"></script>
    <link href="../Comm/JS/Calendar/skin/WdatePicker.css" rel="stylesheet" type="text/css" />
    <script src="Gener.js" type="text/javascript"></script>
     <base target="_self" />
     <style type="text/css">
           .datagrid-cell, .datagrid-cell-group, .datagrid-header-rownumber, .datagrid-cell-rownumber
            {
                text-overflow: ellipsis;
            }
     </style>
    <script language="javascript" type="text/javascript" >

        //定义公共个变量.
        var webUser = new WebUser();

        var ensName = GetQueryString("EnsName");

        //当前用户页面信息.
        var ur = new Entity("BP.Sys.UserRegedit");
        ur.MyPK = webUser.No + "_" + ensName + "_SearchAttrs";
        ur.RetrieveFromDBSources();

        var mapBase;

        //页面启动函数.
        $(function () {

            if (webUser.No == undefined) {
                alert('登录信息丢失，请重新登录。');
                return;
            }

            if (ensName == null || ensName == undefined) {
                $("#Msg").html("必要的参数EnsName没有传入.");
                return;
            }

            $("#Msg").html("<img src=../Img/loading.gif />&nbsp;正在加载,请稍后......");
            
             $("#dialogExpFile").hide();
            //初始化工具栏.
            InitToolbar();


            //执行查询.
             BindTable();

            //生成翻页.
            GenerPageIdx();
            $("#Msg").html("");
        });

        //初始化数据.
        function InitToolbar() {

            //创建处理器.
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_Comm");
            handler.AddUrlData();  //增加参数.

            //获得map基本信息.
            mapBase = handler.DoMethodReturnJSON("Search_MapBaseInfo");

            // $("#Title").html(mapBase.EnDesc); 
            document.title = mapBase.EnDesc;

            //获得查询信息，包含了查询数据表.
            var data = handler.DoMethodReturnJSON("Search_SearchAttrs");

            var html = "";
            if (mapBase.IsShowSearchKey == "1")
                html += "<label for='TB_Key'>关键字</label><input  style='width:100px;' type=text id='TB_Key' name='TB_Key' value='" + ur.SearchKey + "' class='form-control' />";

            if (mapBase.DTSearchWay != "0") {

                html += mapBase.DTSearchLable;

                if (mapBase.DTSearchWay == "1") {
					//html += '<div class="form-group">';
                    html += 	"<input type=text id='TB_DTFrom' name='TB_DTFrom' value='" + ur.DTFrom + "'  onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd'})\" style='width:100px;' class='form-control' />";
                    html += "到<input type=text id='TB_DTTo' name='TB_DTTo' value='" + ur.DTTo + "' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd'})\" style='width:100px;' class='form-control' />";
					//html += '</div>';
                } else {
					//html += '<div class="form-group">';
                    html += 	"<input type=text id='TB_DTFrom' name='TB_DTFrom' value='" + ur.DTFrom + "'  onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'});\" style='width:95px;' class='form-control' />";
                    html += 	"到<input type=text id='TB_DTTo' name='TB_DTTo' value='" + ur.DTTo + "' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'});\"  style='width:95px;' class='form-control' />";
					//html += '</div>';
                }
            }

            //绑定外键枚举查询条件.
            var attrs = data["Attrs"];

            //格式为: @WFSta=0@FK_Dept=02
            var json = AtParaToJson(ur.Vals);

            $("#toolbar").html(html); //设置基础信息.
            for (var i = 0; i < attrs.length; i++) {

                var attr = attrs[i];
                var str = "";
                str += "&nbsp;&nbsp;&nbsp;&nbsp;<label for='DDL_" + attr.Field + "'>" + attr.Name + "</label><select class='form-control' name='DDL_" + attr.Field + "' ID='DDL_" + attr.Field + "'>" + InitDDLOperation(data, attr, "all") + "</select>";
                str = $(str);
                $("#toolbar").append(str); //设置基础信息.
            }


            //为查询外键赋值.
            for (var i = 0; i < attrs.length; i++) {
                var attr = attrs[i];
                var selectVal = json[attr.Field];

                if (selectVal == undefined || selectVal == "")
                    selectVal = "all";

                $("#DDL_" + attr.Field).val(selectVal);
            }

            //是否可以新建？ 
            if (mapBase.IsInsert == "True" ) {
              $("#newWin").show();
            }

        }

        //初始化下拉列表框的OPERATION
        function InitDDLOperation(frmData, mapAttr, defVal) {

            var operations = "";
            operations += "<option value='all' >全部</option>";

            var ens = frmData[mapAttr.Field];
            for (var i = 0; i < ens.length; i++) {

                var en = ens[i];

                if (en.No == undefined )
                    operations += "<option value='" + en.IntKey + "'>" + en.Lab + "</option>";
                else
                    operations += "<option value='" + en.No + "'>" + en.Name + "</option>";
            }
            return operations;
        }

        //执行查询.
        function Search() {

            //保存查询条件.
            var ensName = GetQueryString("EnsName");
            var ur = new Entity("BP.Sys.UserRegedit");
            ur.MyPK = webUser.No + "_" + ensName + "_SearchAttrs";
            ur.FK_Emp = webUser.No;

            if ($("#TB_Key") != null && $("#TB_Key").val()!="")
                ur.SearchKey = $("#TB_Key").val();
           else
              ur.SearchKey = "";

            //设置查询时间.
            if ($("#TB_DTFrom").length == 1)
                ur.DTFrom = $("#TB_DTFrom").val();

            if ($("#TB_DTTo").length == 1)
                ur.DTTo = $("#TB_DTTo").val();

            //获得外键的查询条件,存储里面去.
            var str = "";
            $("select[name^='DDL_']").each(function () {
                var id = $(this).attr("id");
                id = id.replace("DDL_", "");
                str += "@" + id + "=" + $(this).val();
            });

            ur.FK_Emp = webUser.No;
            ur.CfgKey = "SearchAttrs";
            ur.Vals = str;
            ur.FK_MapData = ensName;
            var i = ur.Save();
            var pager = $("#dg").datagrid('getPager').data("pagination" ).options;  
            var pageIndex =  pager.pageNumber;
            window.location.href = "?EnsName=" + ensName + "&PageIdx="+pageIndex;
        }

        function Exp() {

            var url = "";
            if (plant == "CCFlow") {
                url = "Do.aspx?EnsName=" + GetQueryString("EnsName") + "&DoType=SearchExp";
                window.open(url);
                return;
            }

           ReDownExpFile();
            
        }

   

        function ReDownExpFile() {
            var fileName = $("*[id$=expFileName]").val();
            document.getElementById("downLoad").href = "../../DataUser/Temp/" + fileName;

            $("#dialogExpFile").show();
            $("#dialogExpFile").dialog({
                height: 300,
                width: 400,
                showMax: false,
                isResize: true,
                modal: true,
                title: "手动下载文件",
                slide: false,
                close: function () {
                },
                buttons: [{ text: '关闭', handler: function () {
                    $('#dialogExpFile').dialog('close');
                }
                }]
            });
        }

        //生成查询页面..
        function BindTable() {

            //$("#dataGrid").html();

            //创建处理器.
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_Comm");

            //获得查询的基本信息, 实体属性 attrs, 实体数据.
            var pageIdx = this.GetQueryString("PageIdx");

            if (pageIdx == "" || pageIdx == undefined)
                pageIdx = "1";

            handler.AddPara("PageIdx", pageIdx);
            handler.AddPara("EnsName", GetQueryString("EnsName"));

            //查询集合
            var data = handler.DoMethodReturnString("Search_SearchIt");
            if (data.indexOf('err@') == 0) {
                alert(data);
                return;
            }

            data = JSON.parse(data);

            var attrs = data["Attrs"];
                    
            if (attrs == undefined) {
                alert('没有取得属性.');
                return;
            }
           
            var keyOfEn="";


            console.log(data);

            var columns = new Array(); ;
            for (var i = 0; i < attrs.length; i++) {
                var attr = attrs[i]; 

                if (attr.UIVisible == 0
                || attr.KeyOfEn == "OID"
                || attr.KeyOfEn == "WorkID"
                || attr.KeyOfEn == "NodeID"
                || attr.KeyOfEn == "MyNum" 
                || attr.KeyOfEn == "MyPK"){
                    keyOfEn=attr.KeyOfEn
                    continue;
                }
              

                var field = attr.KeyOfEn;
                var title = attr.Name;

                if (attr.UIContralType == 1)
                    field = field + "Text";

                 columns.push({
		                field: field,
		                title: title,
                        fixed:false
		         });
            }


            /**columns.push({
                         field: keyOfEn,
		                title: "操作",
                        fixed:true,
                        width:120,
                        formatter:function(value,row,index){
                           var html="";
                           for(var i=0;i<refMethod.length;i++){
                          
                             //
                             html+="<a href='#' onclick='javaScript:RefMethod1(\'/\',"+index+","+refMethod[i].Target+","+GetQueryString('EnsName')+",xx)'>";
                             html+="<img src="+refMethod[i].Icon+" border='0'/>";
                             html+=refMethod[i].Title+"</a>";
                           }
                           return html;
                        } 
                        
            });*/

            $('#dg').datagrid({
                data: data["DT"],
                columns: [columns],
                fitColumns: false,
                fit: true,
                rownumbers: true,
                striped: true,
                singleSelect: true,
                pagination: true,
                pagePosition: 'bottom',
                toolbar: "#tb",
                loadFilter: pagerFilter,
                onDblClickRow: function (index, rowData) {
                    var pkval = rowData["No"];
                    var objectID = "No";
                    if (pkval == undefined) {
                        pkval = rowData["OID"];
                        objectID = "OID";
                    }
                    if (pkval == undefined) {
                        pkval = rowData["MyPK"];
                        objectID = "MyPK";
                    }
                    if (pkval == undefined) {
                        pkval = rowData["WorkID"];
                        objectID = "WorkID";
                    }
                    if (pkval == undefined) {
                        pkval = rowData["NodeID"];
                        objectID = "NodeID";
                    }
                    var paras = "&" + objectID + "=" + pkval;
                    for (var i = 0; i < attrs.length; i++) {
                        var attr = attrs[i];
                        if (attr.UIContralType == 1)
                            paras += "&" + attr.KeyOfEn + "=" + rowData[attr.KeyOfEn];
                    }

                    OpenEn(pkval, paras);
                }

            });
        }

         //分页功能      
            function pagerFilter(data) {  
                if (typeof data.length == 'number' && typeof data.splice == 'function') {  
                    data = {  
                        total: data.length,  
                        rows: data  
                    }  
                }  
                var dg = $(this);  
                var opts = dg.datagrid('options');  
                var pager = dg.datagrid('getPager');  
                pager.pagination({  
                    onSelectPage: function (pageNum, pageSize) {  
                        opts.pageNumber = pageNum;  
                        opts.pageSize = pageSize;  
                        pager.pagination('refresh', {  
                            pageNumber: pageNum,  
                            pageSize: pageSize  
                        });  
                        dg.datagrid('loadData', data);  
                    }  
                });  
                if (!data.originalRows) {  
                    if(data.rows)  
                        data.originalRows = (data.rows);  
                    else if(data.data && data.data.rows)  
                        data.originalRows = (data.data.rows);  
                    else  
                        data.originalRows = [];  
                }  
                var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);  
                var end = start + parseInt(opts.pageSize);  
                data.rows = (data.originalRows.slice(start, end));  
                return data;  
            } 

        function operate(value, row, index) {  
            return '<a href="#" onclick="editUser('+index+')">查看明细</a>';  
        }  

        function OpenEn(pk,paras) {
            var ensName = GetQueryString("EnsName");
            var enName = ensName.substring(0, ensName.length - 1);

            var cfg=new Entity("BP.Sys.EnCfg",ensName);

            var url=cfg.GetPara("WinOpenUrl");
            if (url=="" || url ==undefined )
            {
              url = "./RefFunc/En2018.htm?EnName=" + mapBase.EnName + "&PKVal=" + pk;
            }
            else
            {
                 if (url.indexOf('?')==-1)
                    url= url+"?1=1";
                 url = url+"&EnsName="+ensName+"&enName="+enName+ "&PKVal=" + pk+paras;
            }

            var windowW = cfg.GetPara("WinCardW");
            if (windowW=="" || windowW==undefined)
                windowW=700;

            var windowH = cfg.GetPara("WinCardH");
             if (windowH=="" || windowH==undefined )
                windowH=500;

            OpenEasyUiDialogExt(url,mapBase.EnDesc + ' : 详细', windowW, windowH, true);
        }

        function New() {
            OpenEn("");
        }

         function Exp() {

            var url = "";
            if (plant == "CCFlow") {
                url = "Do.aspx?EnsName=" + GetQueryString("EnsName") + "&DoType=SearchExp";
                window.open(url);
                return;
            }

            alert("尚未实现");
            window.location.href = data;
        }


        function GenerPageIdx() {

            var handler = new HttpHandler("BP.WF.HttpHandler.WF_Comm");
            handler.AddPara("EnsName", GetQueryString("EnsName"));
            handler.AddPara("PageIdx", this.GetQueryString("PageIdx"));

            var html= handler.DoMethodReturnString("Search_GenerPageIdx");
            $("#end").html(html);
        }

        function Setting()
        {
           var user=new WebUser();
           var url="./Sys/SearchSetting.htm?EnsName="+GetQueryString("EnsName")+"&m="+Math.random();
            OpenEasyUiDialogExt(url,  '设置', 800, 500, true);
        }

    </script>
</head>
<body>

<form id="cc">
 <div data-options="region:'center'" style="padding: 0px;" border="false" style="overflow: hidden;">
        <div class="easyui-panel" style="height:600px">
        <div id="tb" style="padding: 3px;">
            <div id="Div1" style="height: 30px;">
                <div id="toolbar" style="float: left;"> 
                </div>
                <div  style="float: left;"> 
                    <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'" onclick="Search()">查询</a>
                     <a id="newWin" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-new'" onclick="New()">新建</a>
                </div>

                <div  style="float: left;">
                    <a href="#"  class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-excel'" onclick="Exp()">导出</a>
                    <a href="#"  class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-4'" onclick="Setting()">设置</a>
                </div> 

            </div>
        </div>
        
        <table id="dg"></table>
       </div>
    </div>
<div id="Msg"></div>
</form>
<div id="dialogExpFile" style="width: 400px; height: 300px;">
        <div style="margin: 20px; color: Blue;">
            提示：如果没有正常导出文件，请手动点击下方按钮进行下载。
            <br />
            <br />
            <a id="downLoad" href="" class="easyui-linkbutton" data-options="iconCls:'icon-save'">
                点击下载</a>
        </div>
    </div>
    <input type="hidden" id="expFileName" runat="server" />
</body>
</html>
